import { C, N } from "../config/commonTypes"
import { Restfull } from "../config/resModel";
import { LSFS } from "../utils/LSFS"
import { KOA_STATIC_CACHE_PATH } from "../config/const";
import * as path from "path";
import { ErrorScriptName } from "../config/resCodeMsg";
import { ScriptListService } from "../service/scriptList"
const dirExist = async (ctx:C, next:N)=> {
   const { scriptName } =  ctx.request.body.scriptBaseInfo;
   console.log("来到了文件夹是否存在");
   
   const PATH = path.resolve(KOA_STATIC_CACHE_PATH,scriptName)

    //先对数据库查询是否存在
    const DBRes = await ScriptListService.scriptExist(scriptName)
    
    const res = await LSFS.isDir(PATH);
    console.log("DBRes || res",DBRes, res);
    
    if(DBRes || res){
        ctx.body = Restfull.faild(ErrorScriptName);
        return
    }
    
      await  next();
    
 
//    await next()
}

export {
    dirExist
}